package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.EquipmentInfo;
import com.hushao.lims.weblims.domain.EquipmentRepertory;
import com.hushao.lims.weblims.domain.EquipmentStorage;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.repository.EquipmentStorageRepository;
import com.hushao.lims.weblims.service.EquipmentRepertoryService;
import com.hushao.lims.weblims.service.EquipmentService;
import com.hushao.lims.weblims.service.EquipmentStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;

/**
 * @author 930588706
 * @date 18/1/22
 */
@Service
public class EquipmentStorageServiceImpl implements EquipmentStorageService {
    @Autowired
    private EquipmentStorageRepository equipmentStorageRepository;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private EquipmentRepertoryService equipmentRepertoryService;

    @Override
    public Page<EquipmentStorage> listEquipmentStorageByTeacherAndEquipmentName(String equipmentName, TeacherInfo teacher, Pageable pageable) {
        equipmentName="%"+equipmentName+"%";
        return equipmentStorageRepository.findByTeacherInfoAndEquipmentInfo_EquipmentNameLike(teacher,equipmentName,pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentStorage saveEquipmentStorage(EquipmentStorage equipmentStorage) throws Exception {
        EquipmentInfo equipmentInfo=null;

        //器材入库（待审核）
        String equipmentName=equipmentStorage.getEquipmentInfo().getEquipmentName();
        String equipmentSpecifications=equipmentStorage.getEquipmentInfo().getEquipmentSpecifications();
        BigDecimal equipmentPrice=equipmentStorage.getEquipmentInfo().getEquipmentPrice();
        equipmentInfo=equipmentService.findByEquipmentNameAndEquipmentSpecificationsAndEquipmentPrice(equipmentName,equipmentSpecifications,equipmentPrice);
        if(equipmentInfo==null){
                //添加耗材信息
            EquipmentInfo c=new EquipmentInfo(equipmentName,equipmentSpecifications,equipmentPrice,null);
            equipmentInfo=equipmentService.save(c);
        }
        equipmentStorage.setEquipmentInfo(equipmentInfo);
        return equipmentStorageRepository.save(equipmentStorage);
    }

    @Override
    public void remove(Long equipmentStorageId) throws Exception {
        EquipmentStorage equipmentStorage =equipmentStorageRepository.getOne(equipmentStorageId);
        if(equipmentStorage==null){
            throw  new Exception("此器材入库记录不存在！");
        }else if(!"0".equals(equipmentStorage.getEquipmentStorageState())){
            throw new Exception("此器材入库已被审核！");
        }
        equipmentStorageRepository.deleteById(equipmentStorageId);
    }

    @Override
    public Page<EquipmentStorage> listEquipmentStorageByEquipmentNameLikeAndStateLike(String equipmentName, String state, Pageable pageable) {
        equipmentName="%"+equipmentName+"%";
        state="%"+state+"%";
        return equipmentStorageRepository.findByEquipmentInfo_EquipmentNameLikeAndAndEquipmentStorageStateLike(equipmentName,state,pageable);
    }

    @Transactional(rollbackFor =Exception.class)
    @Override
    public EquipmentStorage audit(Long equipmentStorageId, String state, TeacherInfo teacher) throws Exception {
        EquipmentStorage equipmentStorage=equipmentStorageRepository.getOne(equipmentStorageId);
        if(equipmentStorage==null){
            throw new Exception("没有此器材入库信息！");
        }
        if(!"0".equals(equipmentStorage.getEquipmentStorageState())){
            throw new Exception("此器材入库信息已被审核！");
        }
        equipmentStorage.setEquipmentStorageState(state);
        equipmentStorage.setEquipmentStorageAudit(teacher);
        return equipmentStorageRepository.save(equipmentStorage);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentStorage achieve(Long equipmentStorageId) throws Exception {
        EquipmentStorage equipmentStorage=equipmentStorageRepository.getOne(equipmentStorageId);
        if(equipmentStorage==null){
            throw new Exception("没有此器材入库信息！");
        }
        if("0".equals(equipmentStorage.getEquipmentStorageState())){
            throw new Exception("此器材入库信息未被审核！");
        }
        if(!"1".equals(equipmentStorage.getEquipmentStorageState())){
            throw new Exception("此器材入库信息未通过！");
        }
        Integer count=equipmentStorage.getEquipmentCount();
        EquipmentInfo equipmentInfo=equipmentStorage.getEquipmentInfo();
        EquipmentRepertory repertory=equipmentRepertoryService.findByEquipment(equipmentInfo);

        if(repertory==null){
            repertory=new EquipmentRepertory(equipmentInfo,count,null);
        }else {
            repertory.setEquipmentCount(repertory.getEquipmentCount()+count);
        }
        equipmentRepertoryService.saveOrUpdate(repertory);
        equipmentStorage.setEquipmentStorageState("3");
        return equipmentStorageRepository.save(equipmentStorage);
    }
}
